package com.travelsmart.client.apresentacao.widget;

import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.ListBox;

public class FormListBox extends FormWidget<String>{
	private ListBox listBox = new ListBox();
	
	public FormListBox(String name, String infoHelp, String[] values){
		this(name, infoHelp, values, null, null);
	}
	
	public FormListBox(String name, String infoHelp, String[] values, final ChangeHandler changeHandler){
		this(name, infoHelp, values, changeHandler, null);
	}
	
	public FormListBox(String name, String infoHelp, String[] values, final ChangeHandler changeHandler, final HandlerValidation<String> handler){
		super(handler);
		if(changeHandler != null){
			listBox.addChangeHandler(changeHandler);	
		}
		for (String string : values) {
			listBox.addItem(string);
		}
		listBox.setName(name);
		panel.add(listBox);
		super.initHelp(infoHelp);
		initWidget(panel);
	}
	
	public String getValue(){
		return listBox.getSelectedIndex() == -1 ? "" : listBox.getValue(listBox.getSelectedIndex());
	}
	
	public int getSelectedValueInt(){
		return Integer.valueOf(listBox.getSelectedIndex() == -1 ? "0" : listBox.getValue(listBox.getSelectedIndex()));
	}
	
	public void getChangeHandler(final ChangeHandler handler){
		listBox.addChangeHandler(handler);
	}
	@Override
	public void setFocus(boolean focused) {
		listBox.setFocus(focused);
		
	}
}
